//商品上架
$(function(){
	//搜索
	$('.searchBar').click(function(){
		var thisform=$(this).parent('form');
		var goods_no=thisform.find('input[name=goods_no]').val();
		var goods_name=thisform.find('input[name=goods_name]').val();
		var bar_code=thisform.find('input[name=bar_code]').val();
		$('#checkStockInfoDatagrid').datagrid('options').pageNumber=1;//设置页码初始值为1
		$('#checkStockInfoDatagrid').datagrid({ 
			queryParams:{
				goods_no:goods_no,
				goods_name:goods_name,
				bar_code:bar_code,
				order_id:order_id
			}
		});
	})
	//更新行数据
	function updateActions(value){
		$.post(confirmEditcheckStockInfoUrl,{id:value.id,check_amount:value.check_amount,check_nonconforming_p:value.check_nonconforming_p,check_scrap_one:value.check_scrap_one,remark:value.remark},function(result){
            if (!result.status){
                $.messager.alert('错误提示',result.message,'error');
            }
        },'json').error(function(data){
        	var info=eval('('+data.responseText+')');
			$.messager.confirm('错误提示',info.message,function(r){
				//$('#authAccessSetDialog').dialog('close');
			});
        });
	};

	var editIndex = undefined;
	function endEditing(){
		if (editIndex == undefined){return true}
		if ($('#checkStockInfoDatagrid').datagrid('validateRow', editIndex)){
			$('#checkStockInfoDatagrid').datagrid('endEdit', editIndex);
			editIndex = undefined;
			return true;
		} else {
			return false;
		}
	};
	//双击行编辑
	function onDblClickRow(index){
		if (editIndex != index){
			if (endEditing()){
				$('#checkStockInfoDatagrid').datagrid('selectRow', index);
				$('#checkStockInfoDatagrid').datagrid('endEdit', editIndex);
				$('#checkStockInfoDatagrid').datagrid('beginEdit', index);
				editIndex = index;
			} else {
				$('#checkStockInfoDatagrid').datagrid('selectRow', editIndex);
			}
		}
	};

	$('#checkStockInfoDatagrid').datagrid({
		//url:getcheckStockInfoUrl+"?randnum="+Math.floor(Math.random()*1000000),
		url:getcheckStockInfoUrl,
		title:'盘库详情',
		fitColumns:true,
		rownumbers:true,
		pageSize:25,
		pageNumber:1,
		pageList:[5,10,15,20,25,30,40,50],
		columns:[[
		{field:'id',title:'id',checkbox:true},
		{field:'manua_id',title:'账册编号',sortable:true},
		{field:'goods_no',title:'商品编码',sortable:true},
		{field:'goods_name',title:'商品名称'},
		{field:'bar_code',title:'条形码'},
		{field:'goods_spec',title:'规格'},
		{field:'amount',title:'库存数量',sortable:true},
		// {field:'conforming_p',title:'合格品',sortable:true},
		{field:'nonconforming_p',title:'残次品',sortable:true},
		{field:'scrap_one',title:'报废品',sortable:true},
		{field:'check_amount',title:'盘库库存数量',sortable:true,
			editor: {
                type: 'numberbox',  
                options: { 
                    required: true,  
                    missingMessage: '不能为空'
                }  
            }
		},
		// {field:'check_conforming_p',title:'盘库合格品',sortable:true,
			// editor: {
                // type: 'numberbox',  
                // options: { 
                    // required: true,  
                    // missingMessage: '不能为空'
                // }  
            // }
		// },
		{field:'check_nonconforming_p',title:'盘库残次品',sortable:true,
			editor: {
                type: 'numberbox',  
                options: { 
                    required: true,  
                    missingMessage: '不能为空'
                }  
            }
		},
		{field:'check_scrap_one',title:'盘库报废品',sortable:true,
			editor: {
                type: 'numberbox',  
                options: { 
                    required: true,  
                    missingMessage: '不能为空'
                }  
            }
		},
		{field:'remark',title:'差异原因',sortable:true,
			editor: {
                type: 'textbox'
            }
		}
		]],
		queryParams:{
				order_id:order_id
		},
		onDblClickRow: onDblClickRow,
		onAfterEdit:function(index,row){
					row.editing = false;
					updateActions(row);
		},
		onLoadError:function(data){
			var info=eval('('+data.responseText+')');
			$.messager.confirm('错误提示',info.message,function(r){
				var tab=$('#Tabs').tabs('getSelected');
				var index=$('#Tabs').tabs('getTabIndex',tab);
				$('#Tabs').tabs('close',index);
			});
		},
		rowStyler:function(index,row){
			if (row.color==0){
				return 'background-color:pink;color:blue;font-weight:bold;';
			}
		},
		pagination:true,
		fit:true,
	});
});
checkStockInfoObj={
	excelBar:function(){//导入EXCEL
		var index=layer.open({
			type: 2,
			title: '导入模板',
			shadeClose: true,
			shade: 0.5,
			offset:['180px'],
			area: ['1020px', '135px'],
			content: excelUrl
		});
	},
	saveBar:function(){
		var editIndex = undefined;
		if (editIndex == undefined){endEditing = 1;}
		if ($('#checkStockInfoDatagrid').datagrid('validateRow', editIndex)){
			$('#checkStockInfoDatagrid').datagrid('endEdit', editIndex);
			editIndex = undefined;
			endEditing = 1;
		} else {
			endEditing = 0;
		}
		if (endEditing == 1){
			$('#checkStockInfoDatagrid').datagrid('acceptChanges');
		}
	},
	editBar:function(){
		var checkStockInfoRow=$('#checkStockInfoDatagrid').datagrid('getSelections');
        if (checkStockInfoRow.length>1){		            	
            $.messager.alert('提示','一次只能修改一条记录!','info');
        }else if(checkStockInfoRow.length==1){            	
        	$('#checkStockInfoEditDialog').dialog('open').dialog('setTitle','编辑商品');
            $('#checkStockInfoEditForm').form('load',checkStockInfoRow[0]);
        }else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	},
	/* infoBar:function(){//查看商品详细
		var checkStockInfoRow=$('#checkStockInfoDatagrid').datagrid('getSelections');
        if (checkStockInfoRow.length>1){
            $.messager.alert('提示','一次只能查看一条记录!','info');
        }else if(checkStockInfoRow.length==1){
			// console.log(checkStockInfoRow[0]['id']);
			var id=checkStockInfoRow[0]['id'];
        	showTab(checkStockInfoInfoUrl,'确认到货',id);
        }else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	}, */
	save:function(url){
		$('#checkStockInfoEditForm').form('submit',{
            url: url,
            onSubmit: function(){
                return $(this).form('validate');
            },
            success: function(result){
            	var result=eval('('+result+')');
                if (!result.status){
                    $.messager.confirm('错误提示',result.message,function(r){
                    	$('#checkStockInfoEditDialog').dialog('close');
                    });
                }else{
                    $('#checkStockInfoEditDialog').dialog('close');      
                    $('#checkStockInfoDatagrid').datagrid('reload');    
                }
            }
        });
	},
	showAll:function(){
		$('#checkStockInfoDatagrid').datagrid({queryParams:{order_id:order_id}});
	},
	reloadBar:function(){
		$('#checkStockInfoDatagrid').datagrid('reload');
	},
	pushBar:function(){
		var checkStockInfoRow=$('#checkStockInfoDatagrid').datagrid('getSelections');
		var text='确定推送吗！';
        if (checkStockInfoRow.length>0){
        	$.messager.confirm('推送提示',text,function(r){
                if (r){
                	var ids=[];
                	for(var i=0;i<checkStockInfoRow.length;i++){
                		ids.push(checkStockInfoRow[i].id);
						// if(checkStockInfoRow[i].push_times > 0){
							// $.messager.confirm('推送提示','商品"'+checkStockInfoRow[i].checkStockInfo_name+'"已经推送，要重新推送吗');
						// }
                	}
					// alert(123);
                	var id=ids.join(',');
					$.post(checkStockInfopushUrl,{id:id},function(result){
						if (result.status){
							$.messager.alert('推送成功',result.info);
						}else{
							$.messager.alert('错误提示',result.info,'error');
						}
					},'json').error(function(data){
						var info=eval('('+data.responseText+')');
						$.messager.confirm('错误提示',info.info,function(r){
							//$('#authAccessSetDialog').dialog('close');
						});
					});
                }
        	});
		}else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	}

}